export default {
  state: {
    type: 'all',
    // list: [{
    //   id: 100,
    //   name: "吃饭",
    //   isDone: true
    // },
    // {
    //   id: 201,
    //   name: "睡觉",
    //   isDone: false
    // },
    // {
    //   id: 103,
    //   name: "打豆豆",
    //   isDone: true
    // },
    // ],
    list: JSON.parse(localStorage.getItem('todos')) || []
  },
  mutations: {
    change(state, id) {
      const obj = state.list.find(item => item.id === id)
      obj.isDone = !obj.isDone
    },
    del(state, id) {
      state.list = state.list.filter(item => item.id !== id)
    },
    add(state, name) {
      state.list.unshift({
        id: +new Date(),
        name,
        isDone: false
      })
    },
    changeIsAll(state, val) {
      state.list.forEach(item => item.isDone = val)
    },
    changeType(state, val) {
      state.type = val
    },
    clear(state) {
      state.list = state.list.filter(item => !item.isDone)
    }
  },
  actions: {
    clearAsync(context) {
      setTimeout(() => {
        context.commit('clear')
      }, 1000)
    }
  },
  getters: {
    num(state) {
      return state.list.filter(item => !item.isDone).length
    },
    isShow(state) {
      return state.list.some(item => item.isDone)
    },
    showList(state) {
      if (state.type === 'do') {
        return state.list.filter(item => !item.isDone)
      } else if (state.type === 'done') {
        return state.list.filter(item => item.isDone)
      } else {
        return state.list
      }
    }
  },
  namespaced: true
}